
#include <iostream>
#include <cstdlib>
#include <assert.h>

#include "corbaAddressServer.h"
#include "CServiceMCP.h"
#include "../tools/log.h"
#include "../SM/CorbaSM.h"
#include <boost/concept_check.hpp>

using namespace std;

int main (int argc,char** args)

{
    cout <<"mcp_test"<<endl;
    try {
        //Initialize CORBA ORB - "orb"
        CORBA::ORB_var orb=CORBA::ORB_init(argc,args);


        CORBA::Object_var POAObj=orb->resolve_initial_references("RootPOA");
        PortableServer::POA_var _poa=PortableServer::POA::_narrow(POAObj.in());

        CORBA::Object_var nsobj =orb->resolve_initial_references("NameService");

        CosNaming::NamingContext_var initnc =CosNaming::NamingContext::_narrow(nsobj);
        try {

            CosNaming::Name name;
            name.length(2);
            name[0].id = CORBA::string_dup("AddressServer");
            name[0].kind = CORBA::string_dup("");
            name[1].id = CORBA::string_dup("MCP");
            name[1].kind = CORBA::string_dup("");
            FILE_LOG(logINFO)<<"Resolving mcp object"<<endl;
// Name aufloesen
            CORBA::Object_var obj = initnc->resolve(name);


            FILE_LOG(logDEBUG) <<"Found mcp object"<<endl;

// Objektzeiger in Accountzeiger umwandeln
            AddressServer::mcp_var MCP = AddressServer::mcp::_narrow(obj);
            ::CORBA::ULong ci;
            char* sior=MCP->allocateSM(ci);
            if (ci==0 && !strcmp(sior,"empty"))
            {
                cout <<"EMPTY!"<<endl;
            }
            else
            {
                FILE_LOG(logINFO)<<"string to object...";
                CORBA::Object_var obj_var=orb->string_to_object (sior);
                FILE_LOG(logINFO)<<"string to object done:"<<sior;
                if (CORBA::is_nil(obj_var))
                {
                    FILE_LOG(logINFO)<<"NIL1 refference received";
                    exit(0);
                }
                FILE_LOG(logINFO)<<"Casting to SMSetup ...";
                SMInterface::SMSetup_var SMsetup=SMInterface::SMSetup::_narrow(obj_var);
                FILE_LOG(logINFO)<<"Casting to SMSetup done";
                if (CORBA::is_nil(SMsetup))
                {
                    FILE_LOG(logINFO)<<"NIL2 refference received";
                    exit(0);
                }
                FILE_LOG(logINFO)<<"Trying to create Scheduler";
                //SMsetup->run();
                SMsetup->createScheduler(sior,2.0);
                SMsetup->createLP(5,"5 4 3 2","mename","lptype","adapter");
            }
            cout <<"received id:" <<ci<<endl;
            cout<<"received sior:"<<sior<<endl;
            MCP->releaseSM(ci);

        }
        catch (CosNaming::NamingContext::NotFound & nf)
        {
            FILE_LOG(logERROR)<<"Caught CosNaming::notFoundException."<<endl;
        }
        catch (CosNaming::NamingContext::CannotProceed & nf)
        {
            FILE_LOG(logERROR)<<"Caught CosNaming::CannotProceed."<<endl;
        }
        catch (CosNaming::NamingContext::InvalidName & nf)
        {
            FILE_LOG(logERROR)<<"Caught CosNaming::InvalidName."<<endl;
        }
        catch (CosNaming::NamingContext::AlreadyBound & nf)
        {
            FILE_LOG(logERROR)<<"Caught CosNaming::AlreadyBound."<<endl;
        }
        catch (CosNaming::NamingContext::NotEmpty & nf)
        {
            FILE_LOG(logERROR)<<"Caught CosNaming::NotEmpty."<<endl;
        }





    }
    catch (CORBA::COMM_FAILURE& ex) {
        cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
             << "object." << endl;
    }

    catch (CORBA::SystemException& se)
    {
        FILE_LOG(logERROR)<<"Caught CORBA::SystemException. "<<endl;
    }
    catch (CORBA::Exception&)
    {
        FILE_LOG(logERROR)<<"Caught CORBA::Exception."<<endl;
    }
    catch (omniORB::fatalException& fe)
    {
        FILE_LOG(logERROR)<<"Caught omniORB::fatalException:"<<endl;
        FILE_LOG(logERROR)<<" file: "<<fe.file()<<endl;
        FILE_LOG(logERROR)<<" line: "<<fe.line()<<endl;
        FILE_LOG(logERROR)<<" mesg: "<<fe.errmsg()<<endl;
    }
    catch (...)
    {
        FILE_LOG(logERROR)<<"Caught unknown exception."<<endl;
    }
    return 0;
}
